package com.imoresoft.agile.exception;

import org.apache.commons.lang.StringUtils;

import com.imoresoft.agile.entity.ResultCode;

public class ProException extends Exception {

	private String type = "";

	private String desc = "";

	private String code = "";

	private String msg = "";

	private String subCode = "";

	private String subMsg = "";

	private Object result = null;

	private static final long serialVersionUID = 650248148101539200L;

	public ProException() {
		this(ResultCode.EXCEPTION, "未知异常");
	}

	public ProException(String desc) {
		this(ResultCode.EXCEPTION, desc);
	}

	public ProException(String type, String desc) {
		this.type = type;
		this.desc = desc;
	}

	public ProException(String type, String desc, Throwable e) {
		super(e);
		this.setType(type);
		this.setDesc(desc);
		this.setCode(type);
		this.setMsg(builderMsgFromThrowable(e));
	}

	public ProException(String type, String desc, String code, String msg) {
		this.type = type;
		this.desc = desc;
		this.code = code;
		this.msg = msg;
	}

	public ProException(String type, String desc, String code, String msg,
			String subCode, String subMsg) {
		this.type = type;
		this.desc = desc;
		this.code = code;
		this.msg = msg;
		this.subCode = subCode;
		this.subMsg = subMsg;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		if (StringUtils.isNotBlank(this.getType())) {
			this.type = type + "-->" + this.getType();
		} else {
			this.type = type;
		}
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		if (StringUtils.isNotBlank(this.getDesc())) {
			this.desc = desc + "-->" + this.getDesc();
		} else {
			this.desc = desc;
		}
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getSubCode() {
		return subCode;
	}

	public void setSubCode(String subCode) {
		this.subCode = subCode;
	}

	public String getSubMsg() {
		return subMsg;
	}

	public void setSubMsg(String subMsg) {
		this.subMsg = subMsg;
	}

	public Object getResult() {
		return result;
	}

	public void setResult(Object result) {
		this.result = result;
	}

	public String toString() {
		return "{TYPE:" + this.getType() + ",DESC:" + this.getDesc() + ";"
				+ "CODE:" + this.getCode() + ",MSG:" + this.getMsg() + ";"
				+ "SUB_CODE:" + this.subCode + ",SUB_MSG:" + this.subMsg + "}";
	}

	public String getMessage() {
		return this.toString();
	}

	private String builderMsgFromThrowable(Throwable e) {
		StringBuilder exceptionMsg = new StringBuilder();
		exceptionMsg.append(e.toString() + "\n");
		StackTraceElement[] elements = e.getStackTrace();
		for (StackTraceElement element : elements) {
			exceptionMsg.append(element.toString());
			exceptionMsg.append("\n");
		}
		return exceptionMsg.toString();
	}
}
